<template>
<div class="product-add pb50">
    <!--添加文章-->
    <el-form size="small" :model="form" ref="form" label-width="100px">
      <div class="common-form">{{$t('plus.coupon_add')}}</div>
      <el-form-item
        :label="$t('plus.coupon_title')"
        prop="cardTitle"
        :rules="[{  required: true,  message: ' '}]"
      >
        <el-input
          v-model="form.cardTitle"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item
        :label="$t('plus.coupon_desc')"
        prop="cardContent"
        :rules="[{  required: true,  message: ' '}]"
      >
        <el-input
          v-model="form.cardContent"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item
        :label="$t('plus.coupon_price')"
        prop="sellPrice"
        :rules="[{  required: true,  message: ' '}]"
      >
        <el-input
          v-model="form.sellPrice"
          type="number"
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item
        :label="$t('plus.market_price')"
        prop="cardPrice"
        :rules="[{  required: true,  message: ' '}]"
      >
        <el-input
          v-model="form.cardPrice"
          type="number"
          class="max-w460"
        ></el-input>
      </el-form-item>
      <!--<el-form-item label="库存：" prop="stock_num" :rules="[{ required: true, message: ' ' }]">
        <el-input v-model="form.stock_num" type="number" class="max-w460"></el-input>
      </el-form-item>-->
      <el-form-item :label="$t('plus.coupon_cover')">
        <div>
          <el-button type="primary" @click="openUpload">{{$t('plus.upload_image_button')}}</el-button>
          <img
            :src="path"
            class="mt10"
            :width="120"
            v-if="isImg"
            :isImg="isImg"
          />
          <!--上传图片组件-->
          <Upload
            v-if="isupload"
            :isupload="isupload"
            @returnImgs="returnImgsFunc"
            >{{$t('plus.upload_image_button')}}</Upload
          >
        </div>
      </el-form-item>
      <el-form-item :label="$t('plus.coupon_category')">
        <el-select v-model="form.categoryId" :placeholder="$t('plus.please_select')">
          <el-option
            v-for="(item, index) in category"
            :key="index"
            :label="item.name"
            :value="item.categoryId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('plus.linked_product')">
        <el-select v-model="form.productId" filterable :placeholder="$t('plus.please_select')">
          <el-option
            v-for="(item, index) in product"
            :key="index"
            :label="item.productName"
            :value="item.productId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('plus.product_spec')" prop="productAttr">
        <el-input
          v-model="form.productAttr"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('plus.coupon_status')">
        <el-radio-group v-model="form.cardStatus">
          <el-radio :label="10">{{$t('plus.shelf_on')}}</el-radio>
          <el-radio :label="20">{{$t('plus.shelf_off')}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('plus.sort')">
        <el-input
          type="number"
          v-model="form.cardSort"
          class="max-w460"
        ></el-input>
        <div class="tips">{{$t('plus.sort_note')}}</div>
      </el-form-item>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button size="small" type="info" @click="cancelFunc">{{$t('plus.cancel_text')}}</el-button>
        <el-button
          size="small"
          type="primary"
          @click="onSubmit"
          :loading="loading"
          >{{$t('plus.submit')}}</el-button
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import CardApi from "@/api/card.js";
import Upload from "@/components/file/Upload.vue";
export default {
  components: {
    /*图片上传*/
    Upload
  },
  data() {
    return {
      /*是否上传图片*/
      isupload: false,
      isImg: false,
      path: "",
      /*form表单数据*/
      form: {
        cardTitle: "",
        categoryId: "",
        imageId: "",
        cardSort: 100,
        cardStatus: 10,
        cardContent: "",
        stockNum: "",
        productId: "",
        cardPrice: "",
        sellPrice: "",
        productAttr: ""
      },
      /*文章类别*/
      category: [],
      product: [],
      loading: false
    };
  },
  created() {
    /*获取列表*/
    this.getCategoryList();
  },
  methods: {
    /*上传*/
    openUpload() {
      this.isupload = true;
    },
    /*获取图片*/
    returnImgsFunc(e) {
      let self = this;
      if (e != null) {
        this.form.imageId = e[0].fileId;
        this.path = e[0].filePath;
        this.isImg = true;
      }
      this.isupload = false;
    },
    /*获取文章类别*/
    getCategoryList() {
      let self = this;
      let Params = {};
      CardApi.toAddCard(Params, true).then(res => {
        self.category = res.data.category;
        if (self.category.length > 0) {
          self.form.categoryId = self.category[0].categoryId;
        }
        self.product = res.data.product;
        if (self.product.length > 0) {
          self.form.productId = self.product[0].productId;
        }
      }).catch(error => {});
    },
    /*添加文章*/
    onSubmit() {
      let self = this;
      let form = self.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          CardApi.addCard(form, true).then(data => {
            self.loading = false;
            ElMessage({
              message: data.msg,
              type: "success"
            });
            self.cancelFunc();
          }).catch(error => {
            self.loading = false;
          });
        }
      });
    },
    /*取消添加，返回文章列表*/
    cancelFunc() {
      this.$router.back(-1);
    }
  }
};
</script>

<style>
.edit_container {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

.ql-editor {
  height: 400px;
}
</style>

